//
//  StatusViewModel.swift
//  JJWeibo
//
//  Created by JJ on 15/9/7.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

private let JJStatusNormalCellID = "JJStatusNormalCellID"
private let JJStatusForwardCellID="JJStatusForwardCellID"

class StatusViewModel: NSObject {
    
    var status:Status
    var userIconUrl:NSURL?{
        return NSURL(string: status.user?.profile_image_url ?? "")
    }
    var userMemerImage:UIImage?{
        
        if status.user?.mbrank>0 && status.user?.mbrank < 7{

            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    
    var userVipImage:UIImage?{
        switch(status.user?.verified ?? -1){
        case 0:return UIImage(named: "avatar_vip")
        case 2,3,5:return UIImage(named: "avatar_enterprise_vip")
        case 220:return UIImage(named: "avatar_grassroot")
        default:return nil
        }
    }
    
    var thumbnailURLs:[NSURL]?
    
    var bmiddleUrls:[NSURL]?{
        
        guard let urls=thumbnailURLs else {
            return nil
        }
        
        var array = [NSURL]()
        
        for url in urls{
            
            let urlString=url.absoluteString.stringByReplacingOccurrencesOfString("/thumbnail/", withString: "/bmiddle/")
            array.append(NSURL(string: urlString)!)
        }
        return array
    }
    
    var rowheight:CGFloat=0
    
    var forwordText:String{
        
        let userName=status.retweeted_status?.user?.name ?? ""
        let text=status.retweeted_status?.text ?? ""
        return "@\(userName):\(text)"
    }
    
    var cellID:String{
        return status.retweeted_status != nil ? JJStatusForwardCellID : JJStatusNormalCellID
    }
    
    init(status:Status){
        self.status=status
        
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
            
            thumbnailURLs = [NSURL]()
            
            for dict in urls {
                thumbnailURLs?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
        }
        
        super.init()
    }
    
    
    override var description:String{
        return Status.description()
        
    }

}
